//
//  GenerateCardViewController.m
//  QRMaster
//
//  Created by myApple on 15/3/31.
//  Copyright (c) 2015年 myApple. All rights reserved.
//

#import "GenerateCardViewController.h"
#import "PromptInfoView.h"
#import "RequestNetWork.h"
#import "shareDimensionalView.h"
#import "ZXMultiFormatWriter.h"
#import "ZXImage.h"
#import "ZXingObjC.h"
#import "LineView.h"
#import <ShareSDK/ShareSDK.h>
#import "DropDownMenuView.h"
#import "PromptInfoView.h"
#import "Reachability.h"
#import <AudioToolbox/AudioToolbox.h>
#import "MZSelectableLabel.h"
#import "OpertionPlist.h"
#import "faveriterCard.h"
#import "anaylsistDimensional.h"
#import "TouchBtnColor.h"
#import "EditShareTimeViewController.h"
#import "StartPageController.h"
#import "setTimeModel.h"
#import "RoomTimeView.h"
#import "InternationalControl.h"
#import "ShareUIView.h"
#import <MessageUI/MessageUI.h>
#import "QuickCardProcess.h"

@interface GenerateCardViewController ()<ShareViewDelegate,UINavigationControllerDelegate,MFMessageComposeViewControllerDelegate>
{
    UIImageView * HouseCardImgView;
    UILabel * houseCardLabel;
    
    UIView * setTimeView;//点击分享时，设置时间的view
    UIImageView * logoImgView;//logo图片
    UIImage * houseCardImg;//二维码图片
    NSData * imgData;
    ShareUIView * shareView;  //分享view
    id<ISSContent> publishContent; //分享内容

    UIView * promtBackGroundView;
    UIView * helpView;
    
    NSString * linkURLNO;
    NSString * sendMobile;    //要分享人的电话号码
    anaylsistDimensional * anaylsistDim;
    
    UIImageView * promtView;//提示操作view
    UIButton * faveritBtn;//收藏按钮
    NSString * roomID;//历史收藏房卡
    UIImageView * promtImgView;//分享提示页面
    UIImageView * faveritImgView;//收藏提示页面
    shareDimensionalView * sdv;
    float value;//记住进来时屏幕的亮度
    NSInteger time;//记住可以分享的最长时间
    UIButton * timeBtn;//查看时间周期
    
    BOOL refreshFlag;//是否刷新要刷新（no表示从分享页面返回不刷新 yes则可以刷新）
    NSData * shareLinkImgData;
    RoomTimeView * timeView;
    UIButton * shareBtn;//总控卡的时候不让其显示
    NSInteger communityManger;//是不是组管理
    NSString *  currentLanagage;
    
    NSString * comName;
    NSString * comId;
    CardInfoModel * cardModel;
    AllFunctionCardModel * functionCardMode;
    NSInteger cardType;//1 房卡 2 功能卡
    
    //多语言
    UILabel * cardLabel;
    UILabel * textLabel;
    MZSelectableLabel * MztextLabel;
    UILabel * helpLabel;
    UILabel * labelOne;
    UILabel * promtTextLabel;
    UILabel * setViewLabel;
}

@end

static SystemSoundID shake_sound_male_id = 0;

static SystemSoundID shake_sound_refresh = 1;

@implementation GenerateCardViewController

-(id)initwithCommuntyId:(NSString *)communityId communityName:(NSString *)communityName model:(CardInfoModel *)model
{
    if (self) {
        // Custom initialization
        refreshFlag = YES;
        shareLinkImgData = UIImageJPEGRepresentation([UIImage imageNamed:@"ic_锁掌柜_70"],1.0);
        communityManger = 0;
        comName = communityName;
        comId = communityId;
        cardModel = model;
        cardType = 1;
    }
    
    return self;
}

-(id)initwithFunctionCardModel:(AllFunctionCardModel *)model
{
    if (self) {
        // Custom initialization
        refreshFlag = YES;
        shareLinkImgData = UIImageJPEGRepresentation([UIImage imageNamed:@"ic_锁掌柜_70"],1.0);
        communityManger = 0;
        comName = [QRSingleton sharedQRSingleton].communityName;
        comId = [QRSingleton sharedQRSingleton].communityId;
        functionCardMode = model;
        cardType = 2;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBA(237, 238, 241, 1);
    self.title = NSLocalizedString(@"qiuckCard", nil);
    self.title = [QRSingleton sharedQRSingleton].communityName;
    [self prepareView];
    [self getCardInfoByPlist];
    [self getCurrentLanagage];
    [self prepareShareView];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hiddenKeyboard) name:@"KCHiddenKeyBoardNotification" object:nil];
//    NSDictionary * communityInfo = [OpertionPlist readDataFromPlist];
//    NSArray * communityArr = [communityInfo objectForKey:COMMUNITYINFOFORKEY];
//    if (communityArr.count > 0) {
//        for (NSDictionary * dict in communityArr) {
//            if ([[dict objectForKey:@"communityid"]isEqualToString:comId]) {
//                communityManger = [[dict objectForKey:@"groupMId"]integerValue];
//                self.title = [dict objectForKey:@"communityname"];
//            }
//        }
//    }
//    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
//    if ([[userInfo objectForKey:@"faverit"]isEqualToString:@"yes"]) {
//        [self preparePromtView];
//    }
    // Do any additional setup after loading the view.
}

-(BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    value = [UIScreen mainScreen].brightness;
    [[UIScreen mainScreen]setBrightness:1.0];
    if (cardType == 2) {
        shareBtn.hidden = YES;
        timeBtn.hidden = YES;
    }
    else
    {
        shareBtn.hidden = NO;
        timeBtn.hidden = NO;
    }
    [self becomeFirstResponder];
    if (refreshFlag) {
        [self getcheckAuthData];
    }
    else
    {
        refreshFlag = YES;
    }
//    cardLabel.text = NSLocalizedString(@"nowCardNum", nil);
    textLabel.text = NSLocalizedString(@"promtUserCard", nil);
    if ([[InternationalControl userLanguage]isEqualToString:@"en"]) {
        MztextLabel.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 200, MztextLabel.frame.origin.y,200, MztextLabel.frame.size.height);
    }
    else
    {
        MztextLabel.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 100, MztextLabel.frame.origin.y,100, MztextLabel.frame.size.height);
    }
    MztextLabel.text = NSLocalizedString(@"cardHelp", nil);
    NSMutableAttributedString * astr = [[NSMutableAttributedString alloc]initWithString:MztextLabel.text];
    [astr addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:12.0],NSFontAttributeName,RGBA(125, 194, 52, 1),NSForegroundColorAttributeName, nil] range:[MztextLabel.text rangeOfString:NSLocalizedString(@"forHelp", nil)]];
    MztextLabel.attributedText = astr;
    [MztextLabel setSelectableRange:[[MztextLabel.attributedText string]rangeOfString:NSLocalizedString(@"forHelp", nil)]];
    helpLabel.text = NSLocalizedString(@"help", nil);
    labelOne.text = NSLocalizedString(@"cardHelpContent", nil);
    promtTextLabel.text = NSLocalizedString(@"noDeflutCard", nil);
    setViewLabel.text = NSLocalizedString(@"shareCardTime", nil);
     [[NSNotificationCenter defaultCenter]postNotificationName:@"selectIndexCause" object:nil];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[UIScreen mainScreen] setBrightness:value];
    shareBtn.hidden = YES;
}

#pragma mark -- 操作提示页面
-(void)preparePromtView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height - 64;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = ScreenHeight;
    
    promtView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    promtView.backgroundColor = RGBA(0, 0, 0, 0.5);
    promtView.userInteractionEnabled = YES;
    [self.view addSubview:promtView];
    UITapGestureRecognizer * promtViewClick = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickPromtView)];
    [promtView addGestureRecognizer:promtViewClick];
    
    x = 50;
    width = ScreenWidth - 60;
    y = 54;
    height = ScreenHeight / 3.0;
    promtImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    promtImgView.image = [UIImage imageNamed:NSLocalizedString(@"shareCard", nil)];
    [promtView addSubview:promtImgView];
    
    x = 40;
    width = ScreenWidth;
    height = 410.0 / 640 * width;
    y = ScreenHeight - height - ScreenHeight / 3.0 + 115;
    if(UI_SCREEN_HEIGHT == 480)
    {
        y = ScreenHeight - height - ScreenHeight / 3.0 + 150;
    }
    faveritImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    faveritImgView.image = [UIImage imageNamed:NSLocalizedString(@"faveritCardPromt", nil)];
    [promtView addSubview:faveritImgView];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    if ([[userInfo objectForKey:@"share"]isEqualToString:@"yes"]) {
        faveritImgView.hidden = YES;
    }
    else
    {
        promtImgView.hidden = YES;
    }
}

-(void)clickPromtView
{
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    if ([[userInfo objectForKey:@"share"]isEqualToString:@"yes"]) {
        promtImgView.hidden = YES;
        faveritImgView.hidden = NO;
        [userInfo setValue:@"no" forKey:@"share"];
        [userInfo synchronize];
    }
    else
    {
        [promtView removeFromSuperview];
        [userInfo setValue:@"no" forKey:@"faverit"];
        [userInfo synchronize];
    }
    
}

#pragma mark  ---- 注册声音到系统
-(void)playSound
{
    NSString * path = [[NSBundle mainBundle]pathForResource:@"5018" ofType:@"wav"];
    if (path) {
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &shake_sound_male_id);
        AudioServicesPlayAlertSound(shake_sound_male_id);
    }
}

-(void)playRefreshSound
{
    NSString * path = [[NSBundle mainBundle]pathForResource:@"shake_match" ofType:@"mp3"];
    if (path) {
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &shake_sound_refresh);
        AudioServicesPlayAlertSound(shake_sound_refresh);
    }
}


-(void)prepareView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = ScreenWidth - 50;
    float y = 2;
    float width = 40;
    float height = 40;
 
    shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    shareBtn.frame = CGRectMake(x, y, width, height);
    [shareBtn addTarget:self action:@selector(ShareClick) forControlEvents:UIControlEventTouchUpInside];
    [shareBtn setBackgroundImage:[TouchBtnColor imageWithColor:[UIColor grayColor] size:shareBtn.frame.size] forState:UIControlStateHighlighted];
    shareBtn.hidden = YES;
    [self.navigationController.navigationBar addSubview:shareBtn];
    
    x = 10;
    y = 10;
    width = 20;
    height = 20;
    UIImageView * shareImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    shareImgView.image = [UIImage imageNamed:@"ic_share"];
    [shareBtn addSubview:shareImgView];
    
    x = 20;
    y = 40;
    width = ScreenWidth - x * 2;
    height = width;
    if (UI_SCREEN_HEIGHT == 480) {
        y = ScreenHeight / 3.0 - 130;
        width = ScreenWidth - x * 2;
        height = width;
    }
    UIImageView * houseCardBackView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    houseCardBackView.userInteractionEnabled = YES;
    houseCardBackView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:houseCardBackView];
    
    //二维码房卡
    x = houseCardBackView.frame.size.width / 2.0 - 100;
    y = houseCardBackView.frame.size.height / 2.0 - 120;
    width = 200;
    height = 200;
    HouseCardImgView = [[UIImageView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    HouseCardImgView.userInteractionEnabled = YES;
    HouseCardImgView.image = [UIImage imageNamed:@"房卡有权限"];
    [houseCardBackView addSubview:HouseCardImgView];
    
    UITapGestureRecognizer * refreshImgTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(refreshClick:)];
    [HouseCardImgView addGestureRecognizer:refreshImgTap];
    
    
    logoImgView = [[UIImageView alloc]initWithFrame:CGRectMake(HouseCardImgView.frame.size.width / 2.0 - 15, HouseCardImgView.frame.size.height / 2.0 - 15, 30, 30)];
    logoImgView.image = [UIImage imageNamed:@"ic_锁掌柜_70"];
    logoImgView.hidden = YES;
    [HouseCardImgView addSubview:logoImgView];
    
    x = 0;
    width = houseCardBackView.frame.size.width;
    y = houseCardBackView.frame.size.height - 40;
    height = 40;
    UIView * cardView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    cardView.backgroundColor = RGBA(125, 194, 52, 1);
    [houseCardBackView addSubview:cardView];
    
    x = 10;
    y = 10;
    height = 20;
    width = 80;
    cardLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    cardLabel.text = NSLocalizedString(@"nowCardNum", nil);
    cardLabel.font = [UIFont systemFontOfSize:14.0];
    cardLabel.textColor = [UIColor whiteColor];
    [cardView addSubview:cardLabel];
    
    x = 80;
    width = cardView.frame.size.width - x - 60;
    houseCardLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    houseCardLabel.font = [UIFont systemFontOfSize:14.0];
    houseCardLabel.textColor = [UIColor whiteColor];
    houseCardLabel.text = cardModel.roomName;
    [cardView addSubview:houseCardLabel];
    
    faveritBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    faveritBtn.frame = CGRectMake(cardView.frame.size.width - 80, 5, 30, 30);
    [faveritBtn setBackgroundImage:[UIImage imageNamed:@"ic_heart"] forState:0];
    [faveritBtn addTarget:self action:@selector(clickImg:) forControlEvents:UIControlEventTouchUpInside];
    [cardView addSubview:faveritBtn];
    
    if (cardType == 2) {
        cardLabel.frame = CGRectMake(cardLabel.frame.origin.x, cardLabel.frame.origin.y, 100, cardLabel.frame.size.height);
        houseCardLabel.frame = CGRectMake(110, houseCardLabel.frame.origin.y, houseCardLabel.frame.size.width, houseCardLabel.frame.size.height);
        faveritBtn.hidden = YES;
        switch (functionCardMode.functionCardStatus) {
            case 1:
                cardLabel.text = NSLocalizedString(@"controlCard", nil);
                houseCardLabel.text = [QRSingleton sharedQRSingleton].communityName;
                break;
            case 2:
                cardLabel.text = NSLocalizedString(@"buildCard", nil);
                houseCardLabel.text = functionCardMode.functionCardName;
                
                break;
            case 3:
                cardLabel.text = NSLocalizedString(@"floorCard", nil);
                self.title = functionCardMode.buildingName;
                houseCardLabel.text = functionCardMode.functionCardName;
                break;
            default:
                break;
        }
    }
    
    timeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    timeBtn.frame = CGRectMake(cardView.frame.size.width - 40, 5, 30, 30);
    [timeBtn setBackgroundImage:[UIImage imageNamed:@"ic_time30white"] forState:0];
    timeBtn.hidden = YES;
    [timeBtn addTarget:self action:@selector(lookTime) forControlEvents:UIControlEventTouchUpInside];
    [cardView addSubview:timeBtn];
    
    x = houseCardBackView.frame.origin.x;
    y = houseCardBackView.frame.origin.y + houseCardBackView.frame.size.height + 5;
    width = cardView.frame.size.width;
    height = 40;
    if (UI_SCREEN_HEIGHT == 480) {
        height = 40;
        y -= 5;
    }
    textLabel = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    textLabel.text = NSLocalizedString(@"promtUserCard", nil);
    textLabel.numberOfLines = 0;
    textLabel.font = [UIFont systemFontOfSize:14.0];
    textLabel.textColor = RGBA(135, 199, 66, 1);
    [self.view addSubview:textLabel];
    
    //帮助提示
    y = textLabel.frame.size.height + textLabel.frame.origin.y + 10;
    if ([[InternationalControl userLanguage]isEqualToString:@"en"]) {
        x = ScreenWidth - 200;
        width = 200;
    }
    else
    {
        x = ScreenWidth - 100;
        width = 100;
    }
    height = 30;
    MztextLabel = [[MZSelectableLabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    MztextLabel.text = NSLocalizedString(@"cardHelp", nil);
    MztextLabel.font = [UIFont systemFontOfSize:12.0];
    MztextLabel.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:MztextLabel];
    
    __weak GenerateCardViewController * weakSelf = self;
    MztextLabel.selectionHandler = ^(NSRange range,NSString * string)
    {
        [weakSelf helps];
    };
    
    NSMutableAttributedString * astr = [[NSMutableAttributedString alloc]initWithString:MztextLabel.text];
    [astr addAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:12.0],NSFontAttributeName,RGBA(125, 194, 52, 1),NSForegroundColorAttributeName, nil] range:[MztextLabel.text rangeOfString:NSLocalizedString(@"forHelp", nil)]];
    MztextLabel.attributedText = astr;
    [MztextLabel setSelectableRange:[[MztextLabel.attributedText string]rangeOfString: NSLocalizedString(@"forHelp", nil)]];
    
    x = 0;
    y = 0;
    width = ScreenWidth;
    height = ScreenHeight - 64;
    promtBackGroundView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    promtBackGroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2];
    promtBackGroundView.hidden = YES;
    [self.view addSubview:promtBackGroundView];
    
    UITapGestureRecognizer * backTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(removeBackView)];
    [promtBackGroundView addGestureRecognizer:backTap];
    
    x = ScreenWidth;
    y = promtBackGroundView.frame.size.height;
    height = 240;
    width =ScreenWidth;
    helpView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    helpView.backgroundColor = [UIColor whiteColor];
    helpView.hidden = YES;
    [promtBackGroundView addSubview:helpView];
    
    timeView = [[RoomTimeView alloc]init];
    timeView.frame = helpView.frame;
    timeView.hidden = YES;
    timeView.backgroundColor = [UIColor whiteColor];
    [promtBackGroundView addSubview:timeView];
    
    
    x = ScreenWidth /10;
    width = 80;
    y = 20;
    height = 20;
    helpLabel =[[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    helpLabel.text = NSLocalizedString(@"help", nil);
    helpLabel.font = [UIFont boldSystemFontOfSize:18.0];
    helpLabel.textColor = RGBA(153, 153, 153, 1);
    helpLabel.textAlignment = NSTextAlignmentCenter;
    [helpView addSubview:helpLabel];
    
    
    y += height + 15;
    width = ScreenWidth - 2 * x;
    height = 120;
    labelOne = [[UILabel alloc]initWithFrame:CGRectMake(x, y, width, height)];
    labelOne.text = NSLocalizedString(@"cardHelpContent", nil);
    labelOne.numberOfLines = 0;
    labelOne.font = [UIFont systemFontOfSize:14.0];
    [helpView addSubview:labelOne];
    
    shareView = [[ShareUIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    shareView.delegate = self;
    shareView.hidden = YES;
    [[UIApplication sharedApplication].keyWindow addSubview:shareView];
    
}

-(void)prepareShareView
{
    float ScreenWidth = [UIScreen mainScreen].bounds.size.width;
    float ScreenHeight = [UIScreen mainScreen].bounds.size.height;
    float x = 0;
    float y = 0;
    float width = ScreenWidth;
    float height = ScreenHeight - y;
    sdv = [[shareDimensionalView alloc]initWithFrame:CGRectMake(x, y, width, height)];
    sdv.hidden = YES;
    [self.view addSubview:sdv];
}

//帮助提示
-(void)helps
{
    promtBackGroundView.hidden = NO;
    helpView.hidden = NO;
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = helpView.frame;
        frame.origin.y = promtBackGroundView.frame.size.height - 200;
        frame.origin.x = 0;
        helpView.frame = frame;
    } completion:^(BOOL finished) {
        
    }];
}

//移除帮助提示
-(void)removeBackView
{
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = helpView.frame;
        frame.origin.y = promtBackGroundView.frame.size.height;
        frame.origin.x = -[UIScreen mainScreen].bounds.size.width;
        helpView.frame = frame;
        CGRect timeFrame = timeView.frame;
        timeFrame.origin.y = promtBackGroundView.frame.size.height;
        timeFrame.origin.x = -[UIScreen mainScreen].bounds.size.width;
        timeView.frame = timeFrame;
    } completion:^(BOOL finished) {
        promtBackGroundView.hidden = YES;
        helpView.hidden = YES;
        timeView.hidden = YES;
        helpView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, helpView.frame.origin.y, helpView.frame.size.width, helpView.frame.size.height);
        timeView.frame = CGRectMake([UIScreen mainScreen].bounds.size.width, helpView.frame.origin.y, helpView.frame.size.width, helpView.frame.size.height);
    }];
}

//点击图片刷新
-(void)refreshClick:(UITapGestureRecognizer *)tap
{
    
    [[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(getcheckAuthData) object:tap];
    [self performSelector:@selector(getcheckAuthData) withObject:tap afterDelay:0.3f];
    //    [self getcheckAuthData];
}

#pragma mark -- 获取当前语言
-(void)getCurrentLanagage
{
    currentLanagage = [InternationalControl userLanguage];
    if ([currentLanagage isEqualToString:@"zh-Hans"]) {
        currentLanagage = @"zh_cn";
    }
    else if([currentLanagage isEqualToString:@"zh-Hant-HK"])
    {
        currentLanagage = @"zh_tw";
    }
    else if([currentLanagage isEqualToString:@"zh-Hant-TW"])
    {
        currentLanagage = @"zh_tw";
    }
    else if([currentLanagage isEqualToString:@"en"])
    {
        currentLanagage = @"en_us";
    }
    else
    {
        currentLanagage = @"zh_cn";
    }
}

-(void)shareType
{
    //构造分享内容
    publishContent  = [ShareSDK content:NSLocalizedString(@"shareContent", nil)
                         defaultContent:NSLocalizedString(@"shareDefualt", nil)
                                  image:[ShareSDK imageWithData:imgData fileName:@"image" mimeType:@"jpg"]
                                  title:NSLocalizedString(@"shareTitle", nil)
                                    url:nil
                            description:nil
                              mediaType:SSPublishContentMediaTypeImage];
    [publishContent addQQUnitWithType:[NSNumber numberWithInt:SSPublishContentMediaTypeNews] content:NSLocalizedString(@"shareContent", nil) title:NSLocalizedString(@"shareTitle", nil) url:[NSString stringWithFormat:@"%@",linkURLNO] image:[ShareSDK imageWithData:shareLinkImgData fileName:@"image" mimeType:@"jpg"]];
    [publishContent addWeixinSessionUnitWithType:[NSNumber numberWithInt:SSPublishContentMediaTypeNews] content:NSLocalizedString(@"shareContent", nil) title:NSLocalizedString(@"shareTitle", nil) url:[NSString stringWithFormat:@"%@",linkURLNO] image:[ShareSDK imageWithData:shareLinkImgData fileName:@"image" mimeType:@"jpg"] musicFileUrl:nil extInfo:nil fileData:nil emoticonData:nil];
    
    shareView.hidden = NO;
    
}

#pragma mark ----- 分享点击图片事件
-(void)shareClick:(NSInteger)shareIndex
{
    [self cancelShare];
    int shareType = 0;
    if (shareIndex == 2) {
        [self sendSMS];
        return;
    }
    else if (shareIndex == 0) {
        shareType = ShareTypeQQ;
    }
    else if (shareIndex == 1)
    {
        shareType = ShareTypeWeixiSession;
    }
    
    [ShareSDK showShareViewWithType:shareType container:nil content:publishContent statusBarTips:YES authOptions:nil shareOptions:nil result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
        if (state == SSResponseStateFail)
        {
            [PromptInfoView showMessage:NSLocalizedString(@"shareFail", nil)] ;
        }
        
    }];
}

#pragma mark ------取消分享
-(void)cancelShare
{
    shareView.hidden = YES;
}

#pragma mark --------  发送短信
-(void)sendSMS
{
    if ([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController * messageController = [[MFMessageComposeViewController alloc]init];
        messageController.delegate = self;
        messageController.messageComposeDelegate = self;
        NSString * messageContent = [NSString stringWithFormat:@"%@%@%@",NSLocalizedString(@"messageFirst", nil), linkURLNO,NSLocalizedString(@"messageSecond", nil)];
        messageController.body = messageContent;
        messageController.recipients = @[sendMobile];
        [self presentViewController:messageController animated:YES completion:^{
            [self cancelShare];
        }];
    }
    else
    {
        [PromptInfoView showMessage:NSLocalizedString(@"shareFail", nil)] ;
    }

}

-(void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)getcheckAuthData
{
    if ([self IsHaveNetwork]) {
        return;
    }
    [self startWaiting:nil color:nil addToView:self.view];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:comId forKey:@"communityId"];
    if (cardType == 1) {
        [postDict setValue:cardModel.bulidingId forKey:@"buildingId"];
        [postDict setValue:cardModel.floorId forKey:@"floorId"];
        [postDict setValue:cardModel.roomId forKey:@"roomId"];
        [postDict setValue:@"0" forKey:@"validTime"];
        [postDict setValue:@"3" forKey:@"type"];
        RequestNetWork * rnk = [[RequestNetWork alloc]init];
        [self startWaiting:nil color:nil addToView:self.view];
        [rnk initWithPath:POST(CHECKAUTHORTIY) params:postDict block:^(id downLoadData, id error) {
            [self stopWaiting];
            [self downLoadDataHandel:downLoadData errorInfo:error];
        }];
    }
    else if (cardType == 2)
    {
        NSString * functionCardType = nil;
        switch (functionCardMode.functionCardStatus) {
            case 1:
                functionCardType = @"master";
                break;
            case 2:
                functionCardType = @"building";
                [postDict setValue:functionCardMode.buildingId forKey:@"buildingId"];
                [postDict setValue:cardModel.floorId forKey:@"floorId"];
                break;
            case 3:
                functionCardType = @"floor";
                [postDict setValue:functionCardMode.buildingId forKey:@"buildingId"];
                [postDict setValue:functionCardMode.floorId forKey:@"floorId"];
                break;
            default:
                functionCardType = @"floor";
                [postDict setValue:functionCardMode.buildingId forKey:@"buildingId"];
                [postDict setValue:functionCardMode.floorId forKey:@"floorId"];
                break;
        }
         [postDict setValue:functionCardType forKey:@"type"];
        RequestNetWork * rnk = [[RequestNetWork alloc]init];
        [self startWaiting:nil color:nil addToView:self.view];
        [rnk initWithPath:POST(FUNCTIONCARD) params:postDict block:^(id downLoadData, id error) {
            [self stopWaiting];
            [self downLoadDataHandel:downLoadData errorInfo:error];
        }];
    }
}

-(void)downLoadDataHandel:(id)downLoadData errorInfo:(id)error
{
    NSDictionary * data = [downLoadData objectForKey:@"data"];
    if (data != nil) {
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"sound"]isEqualToString:@"1"]) {
            [self playRefreshSound];
        }
        NSString * string = [data objectForKey:@"cardString"];
        anaylsistDim = [[anaylsistDimensional alloc]init];
        NSString * encodeString = [anaylsistDim stringFromHexString:string];
        ZXImage * image = [anaylsistDim prepareCardView:encodeString addImgView:HouseCardImgView];
        if (image)
        {
            HouseCardImgView.image = [UIImage imageWithCGImage:image.cgimage];
            houseCardImg = [UIImage imageWithCGImage:image.cgimage];
            imgData = UIImageJPEGRepresentation(houseCardImg, 1.0);
            logoImgView.hidden = NO;
        }
        else
        {
            HouseCardImgView.image = [UIImage imageNamed:@"splash"];
            houseCardImg = nil;
            logoImgView.hidden = YES;
        }
    }
    else if (error != nil) {
        logoImgView.hidden = YES;
        HouseCardImgView.image = [UIImage imageNamed:@"splash"];
        houseCardImg = nil;
        [PromptInfoView showMessage:[error objectForKey:@"info"]];
    }
    
}

-(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    //播放声音
    [self playSound];
}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
{
    [self getcheckAuthData];
}

//分享点击事件
-(void)ShareClick
{
    if ([cardModel.roomId isEqualToString:[NSString stringWithFormat:@"-%@",comId]]) {
        [PromptInfoView showMessage:NSLocalizedString(@"shareAfterRefresh", nil)];
    }
    if (communityManger == 1) {
        [PromptInfoView showMessage:NSLocalizedString(@"你暂时没有分享权力", nil)];
        return;
    }
    EditShareTimeViewController * estvc = [[EditShareTimeViewController alloc]init];
    estvc.communityId = comId;
    estvc.roomId = cardModel.roomId;
    estvc.surePost = ^(NSString * result,NSString * mobile)
    {
        refreshFlag = NO;
        linkURLNO = result;
        sendMobile = mobile;
        [self shareType];
    };
    [self.navigationController pushViewController:estvc animated:YES];
}

-(void)getCardInfoByPlist
{
    NSMutableDictionary * dict = [OpertionPlist readDataFromPlist];
    NSMutableArray * cardInfoArray = [dict objectForKey:FAVERITCARD];
    for (NSDictionary * cardDict in cardInfoArray) {
        if ([[cardDict objectForKey:@"roomId" ] isEqualToString:cardModel.roomId]) {
            [faveritBtn setBackgroundImage:[UIImage imageNamed:@"ic_heartfilled"] forState:0];
        }
        
    }
}

#pragma mark -  点击取消收藏或者收藏
-(void)clickImg:(UIButton *)btn
{
    NSMutableDictionary * dict = [[NSMutableDictionary alloc]init];
    [dict setValue:comId forKey:@"communityId"];
    [dict setValue:comName forKey:@"communityName"];
    [dict setValue:cardModel.bulidingId forKey:@"buildId"];
    [dict setValue:cardModel.bulidingName forKey:@"buildName"];
    [dict setValue:cardModel.floorId forKey:@"floorId"];
    [dict setValue:cardModel.floorName forKey:@"floorName"];
    [dict setValue:cardModel.roomId forKey:@"roomId"];
    [dict setValue:cardModel.roomName forKey:@"roomName"];
    if (_isCollectFlag) {
        QuickCardProcess * process = [[QuickCardProcess alloc]init];
        [process cancelCollectionCard:dict block:^(NSString *info, NSString *errorInfo) {
            if (info) {
                [PromptInfoView showMessage:info superView:self.view];
            }
            else
            {
                [PromptInfoView showMessage:errorInfo superView:self.view];
            }
        }];
    }
    else
    {
        [faveriterCard faveriterOrCancelCardByRoomId:dict result:^(cardResultType type) {
            switch (type) {
                case KCSUCCESS_FAVERITER:
                    [PromptInfoView showMessage:NSLocalizedString(@"collectCardSuccess", nil)];
                    [btn setBackgroundImage:[UIImage imageNamed:@"ic_heartfilled"] forState:0];
                    break;
                case KCFAIL_FAVERITER:
                    [PromptInfoView showMessage:NSLocalizedString(@"collectCount", nil)];
                    break;
                case KCCANCEL_FAVERITER:
                    [PromptInfoView showMessage:NSLocalizedString(@"cancleCardCollect", nil)];
                    [faveritBtn setBackgroundImage:[UIImage imageNamed:@"ic_heart"] forState:0];
                    break;
            }
        }];
    }
    
}

//显示时间
-(void)lookTime
{
    //先还要判断下是不是管理员以及是不是总控卡
    if ([cardModel.roomId isEqualToString:[NSString stringWithFormat:@"-%@",comId]]) {
        [PromptInfoView showMessage:NSLocalizedString(@"managerOpenTime", nil)];
        return;
    }
    if (communityManger == 1) {
         [PromptInfoView showMessage:NSLocalizedString(@"managerOpenTime", nil)];
        return;
    }
    if ([self IsHaveNetwork]) {
        return;
    }
    [self startWaiting:nil color:nil addToView:self.view];
    NSUserDefaults * userInfo = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc]init];
    [postDict setValue:[userInfo objectForKey:@"uid"] forKey:@"id"];
    [postDict setValue:[userInfo objectForKey:@"token"] forKey:@"token"];
    [postDict setValue:comId forKey:@"communityId"];
    if (cardType == 1) {
         [postDict setValue:cardModel.roomId forKey:@"roomId"];
    }
   else
   {
        [postDict setValue:[NSString stringWithFormat:@"-%@",[QRSingleton sharedQRSingleton].communityId] forKey:@"roomId"];
   }
    RequestNetWork * rnk = [[RequestNetWork alloc]init];
    [rnk initWithPath:POST(GETMYSELFROOMTINE) params:postDict block:^(id downLoadData, id error) {
        [self stopWaiting];
        NSDictionary * data = [downLoadData objectForKey:@"data"];
        if (data != nil) {
            if ([[data objectForKey:@"flag"]intValue] == 0) {
                NSMutableArray * timeDataArray = [[NSMutableArray alloc]init];
                NSArray * listArray = [data objectForKey:@"list"];
                for (NSDictionary * dict in listArray) {
                    setTimeModel * model = [[setTimeModel alloc]init];
                    model.beginTime = [dict objectForKey:@"startTime"];
                    model.endTime = [dict objectForKey:@"endTime"];
                    model.weekInfo = [dict objectForKey:@"cycleString"];
                    model.timeId = [dict objectForKey:@"id"];
                    [timeDataArray addObject:model];
                }
                if (timeDataArray.count == 0) {
                    [PromptInfoView showMessage:NSLocalizedString(@"noTimeOpenRoom", nil)];
                }
                else
                {
                    promtBackGroundView.hidden = NO;
                    timeView.hidden = NO;
                    [UIView animateWithDuration:0.5 animations:^{
                        CGRect frame = timeView.frame;
                        frame.origin.y = promtBackGroundView.frame.size.height - (timeDataArray.count * 55 + 80);
                        frame.origin.x = 0;
                        timeView.frame = frame;
                        timeView.timeData = timeDataArray;
                    } completion:^(BOOL finished) {
                        
                    }];
                }
            }
            else
            {
                [PromptInfoView showMessage:NSLocalizedString(@"managerOpenTime", nil)];
            }
        }
        else
        {
            [PromptInfoView showMessage:[error objectForKey:@"info"]];
            
        }
        
    }];
}

-(void)hiddenKeyboard
{
    [self resignFirstResponder];
}


-(void)dealloc
{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"checkAuth" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
